package com.nlgc365.designpattern.Chapter04_Pattern.Creation.P01_Singleton.Usage.IDGenerators;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 通过静态内部类的方式, 保证有一个实例
 * 原理:
 *  通过jvm的类加载机制, 保证类只被加载一次;
 *
 */
public class IdGenerator6 {
    private AtomicLong atomicLong = new AtomicLong(0);

    private static class SingletonHolder {
        private static IdGenerator6 instance = new IdGenerator6();
    }

    public static IdGenerator6 getInstance() {
        return SingletonHolder.instance;
    }

    public long getId() {
        return atomicLong.getAndIncrement();
    }
}
